import * as Cesium from "cesium";
/**
 * @cesium初始化
 * @viewer cesium实例存储对象
 * */
export const initMap = (viewer) => {
  Cesium.Ion.defaultAccessToken =
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmYjMxMzNiMC01Y2I3LTRlZDYtODZmYS1iNjE3ZWRmNTcxMDUiLCJpZCI6MTcyNzUsImlhdCI6MTcwMTc0ODQ5M30.NFQD24HPgC-34__kwexQK6bdgwJKxJwdBex_BlxvZpE";

  // const widget = new Cesium.CesiumWidget("cesiumContainer");
  viewer = new Cesium.Viewer("cesiumContainer", {
    // imageryProvider: esri,

    //图层控件显隐控制
    timeline: false, //隐藏时间轴
    animation: false, //隐藏动画控制器
    geocoder: false, //隐藏地名查找控制器
    homeButton: false, //隐藏Home按钮
    sceneModePicker: false, //隐藏投影方式控制器
    baseLayerPicker: false, //隐藏图层选择控制器
    navigationHelpButton: false, //隐藏帮助按钮
    fullscreenButton: false, //隐藏全屏按钮
  });

  // 添加矢量底图与矢量标注图层
  addMapStyle(viewer, "vec");
  addMapStyle(viewer, "cva");
  // 开场动画，旋转到目标位置
  flyToPosition(viewer, { lng: 120.1715, lat: 30.2739, height: 150000 }, 3);
  // 拾取坐标点，并添加到地图
  getClickPointAdd(viewer)

  // console.log(viewer);
  // console.log(viewer.entities);
  // console.log(viewer.imageryLayers);
};

/**
 * 为cesimu添加天地图的底图
 * @param viewer cesium实例
 * @param layer  底图样式
 * vec：矢量底图、cva：矢量标注、img：影像底图、cia：影像标注
 * ter：地形晕渲、cta：地形标注、eva：矢量英文标注、eia：影像英文标注
 */
export function addMapStyle(viewer, layer) {
  // 添加天地图影像注记底图
  const tMapImagery = new Cesium.WebMapTileServiceImageryProvider({
    url: `http://t0.tianditu.gov.cn/${layer}_w/wmts?tk=4999d7c944e5f23def8cff1e1e7b880b`,
    layer,
    style: "default",
    tileMatrixSetID: "w",
    format: "tiles",
    maximumLevel: 18,
  });
  viewer.imageryLayers.addImageryProvider(tMapImagery);
}

/**
 * @description : 初始场景动画，飞到目标点
 * @param {*} viewer 
 * @param {*} position ：目标点位置
 * @param {*} duration ：持续时间
 * @return {*}
 */
export function flyToPosition(viewer, position, duration) {
  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(position.lng, position.lat, position.height),
    duration: duration,
  });
}



/**
 * @description: 获取当前鼠标点击位置坐标，并添加到图上显示
 * @param {*} _viewer
 * @return {*}
 */
function getClickPointAdd(viewer) {
  // 注册屏幕点击事件
  let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  handler.setInputAction(function (event) {
    // 转换为不包含地形的笛卡尔坐标
    let clickPosition = viewer.scene.camera.pickEllipsoid(event.position);
    // 转经纬度（弧度）坐标
    let radiansPos = Cesium.Cartographic.fromCartesian(clickPosition);
    // 转角度
    console.log("经度：" + Cesium.Math.toDegrees(radiansPos.longitude) + ", 纬度：" + Cesium.Math.toDegrees(radiansPos.latitude));

    // 添加点
    viewer.entities.add({
      position: clickPosition,
      point: {
        color: Cesium.Color.YELLOW,
        pixelSize: 30
      }
    })
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}


/**
 * @description: 绘制多边形
 * @param {*} _viewer
 * @return {*}
 */
export function drawPolygon(viewer) {
  console.log(viewer);


  const polygon = viewer.entities.add({
    polygon: {
      hierarchy: new Cesium.PolygonHierarchy(
        Cesium.Cartesian3.fromDegreesArray([
          -118.0,
          30.0,
          -115.0,
          30.0,
          -117.1,
          31.1,
          -118.0,
          33.0,
        ])
      ),
      // 离地高度
      height: 300000.0,
      extrudedHeight: 700000.0,
      outline: true,
      outlineColor: Cesium.Color.WHITE,
      outlineWidth: 4,
      material: Cesium.Color.fromRandom({ alpha: 1.0 }),
    },
  });
  return polygon;
}